// pages/myOrder/myOrder.js
const app = getApp()
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:app.globalData.userInfo,
    showFlag:false,
    showFlags:false,
    flag:true,
    
    style:['rgba(253,174,63,1);','rgba(100,203,117,1);;','rgba(100,203,117,1);','rgba(170,178,199,1);;','rgba(253,174,63,1)'],
    hasTop:true,
    firstFlag:true,
    firstSwith: true,
    listData: [],
		requesting: false,
		end: false,
		emptyShow: false,
		refreshSize: 66,
		topSize: 0,
		bottomSize: 173,
    empty: false,
    
    providentFund:[],//公积金订单
    providentFundRequesting: false,
		providentFundEnd: false,
		providentFundEmptyShow: false,
    providentFundRefreshSize: 66,
		providentFundTopSize: 0,
		providentFundBottomSize: 173,
    providentFundEmpty: false,
    
    payFlag:true,
  },

  // 点击删除
  deletOrder(e){
    let order_id = e.currentTarget.dataset.orderid
    let index = e.currentTarget.dataset.idx
    wx.showModal({
      content: '确定删除该订单吗？',
      success (res) {
        if (res.confirm) {
          app.request('withdraw_cash/delete',{order_id},true).then(res => {
            if(res.error_code == 0){
              let list = that.data.providentFund
              list.splice(index, 1)
              that.setData({
                providentFund:list
              })
              if(list.length == 0 ){
                that.setData({
                  showFlags:true
                })
              }
            }else{
              wx.showModal({
                content: res.error_message,
                showCancel: false
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 公积金订单详情
  providentFundBtn(e){
    let orderid = e.currentTarget.dataset.orderid
    let index = e.currentTarget.dataset.idx
    wx.navigateTo({
      url: `../money_pinkup/pages/showOrder/showOrder?orderid=${orderid}&index=${index}`,
    })
  },
  // 代缴订单详情
  orderDetail(e){
    let orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `../orderMsg/orderMsg?order_id=${orderid}`,
    })
  },
  // 点击代缴订单
  payOrder () {
    if(this.data.firstFlag) return
    this.setData({
      firstFlag: true
    })
    if(this.data.payFlag){
      this.setData({
        payFlag:false,
        refreshSize:66,
      })
      this.pay(0)
    }
  },
  // 点击公积金提取
  extract () {
    if(!this.data.firstFlag) return
    this.setData({
      firstFlag: false
    })
    if(this.data.firstSwith){
      this.setData({
        firstSwith:false,
        providentFundRefreshSize:66,
      },this.providentFund(0))
    }
  },
  // 公积金订单请求
  providentFund(status,count = 0){
    this.setData({
      providentFundRequesting:true
    })
    wx.nextTick(() => {
      let msg = {}
      status == 1? msg.page_size = count : msg
      app.request("withdraw_cash/order_list",msg,true).then(res=>{
        if(res.error_code==0){
          let list = []
          if(status === 0){
            this.setData({
              providentFund: []
            })
            list = res.data.order
          }else{
            list = this.data.providentFund.concat(res.data.order)
            console.log(list)
          }
          console.log(list)
          wx.nextTick(() => {
            this.setData({
              goldCurrent_page:res.data.current_page,
              goldPage_count: res.data.page_count,
              providentFundRequesting:false,
              providentFund:list,
              showFlags: list.length == 0?true:false,
            })
          })
        }
      })
    })
  },
  // 代缴订单请求
  pay(status,count = 0){
    this.setData({
      requesting:true
    })
    let msg = {}
    status == 1? msg.page_size = count : msg
    app.request("daijiao/order", msg, true).then(res => {
      if (res.error_code == 0) {
        let list = []
        // debugger
        if(status === 0){
          console.log(1111)
          this.setData({
            listData:[]
          })
          list = res.data.list
        }else{
          console.log(res.data)
          list = this.data.listData.concat(res.data.list)
          console.log(list)
        }
        wx.nextTick(() => {
          this.setData({
            socialCurrent_page:res.data.current_page,//当前页
            socialPage_count: res.data.page_count,//总页数
            listData: list,
            showFlag: list.length == 0?true:false,
            // listData: [],

            requesting:false
          })
        })
      }
    })
  },
  //下拉刷新 
  refresh(){
    // 当前请求代缴订单
    if(this.data.firstFlag){
        this.pay(0)
        this.setData({
          loadFlag: false
        })
    }else{
      this.providentFund(0)
      this.setData({
        loadFlags: false
      })
    }
  },
  // 加载更多
  more(){
    if(this.data.firstFlag){
      if(this.data.socialCurrent_page < this.data.socialPage_count){
        this.pay(1,this.data.socialCurrent_page+1)
      }else{
        this.setData({
          loadFlag:true
        })
      }
    }else{
      if(this.data.goldCurrent_page < this.data.goldPage_count){
        this.providentFund(1,this.data.socialCurrent_page+1)
      }else{
        this.setData({
          loadFlags:true
        })
      }
      
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    if(options.flag){
      this.extract()
    }else{
      this.pay(0)
    }
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "社保/公积金代缴、公积金提取!",
      path: '/pages/home/home',
      imageUrl: 'https://head.image.jiajijie.com/dj/images/fiveshareBg.png',
      success: function (res) {
      },
      fail: function (res) {
        console.log(res)
      }
    }
  }
})